package com.lyj.mp.controller;


import cn.hutool.core.bean.BeanUtil;
import com.lyj.mp.domain.dto.PageDTO;
import com.lyj.mp.domain.dto.UserFormDTO;
import com.lyj.mp.domain.po.User;
import com.lyj.mp.domain.query.UserQuery;
import com.lyj.mp.domain.result.Result;
import com.lyj.mp.domain.vo.UserVO;
import com.lyj.mp.service.IUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Api(tags = "用户管理")
@RestController
@RequestMapping("/users")
@Slf4j
@RequiredArgsConstructor
// 生成一个包含所有final字段或者带有@NonNull注解的字段的构造器
// Lombok通过反射机制自动生成这个构造器，避免了手动编写冗长的构造器代码
// 这样做可以提高代码的简洁性和可维护性，同时确保所有必须初始化的字段都被正确初始化
public class UserController {

    private final IUserService userService;

    @ApiOperation("新增用户接口")
    @PostMapping("/insert")
    public Result insertUser(@RequestBody UserFormDTO userDto) {
        // 1、先把dto转成po  hutool工具包的
        User user = BeanUtil.copyProperties(userDto, User.class);
        userService.save(user);
        return Result.success();
    }

    @ApiOperation("删除用户接口")
    @DeleteMapping("/delete/{id}")
    public Result deleteUserById(@ApiParam("用户id") @PathVariable("id") Long id) {
        boolean toDelete = userService.removeById(id);
        if (toDelete) {
            return Result.success();
        }
        return Result.error("删除失败");
    }

    @ApiOperation("根据id查询用户信息和地址信息")
    @GetMapping("/select/{id}")
    public Result selectById(@ApiParam("用户id") @PathVariable("id") Long id) {
//        User user = userService.getById(id); 简单查询
//        把po拷贝到vo传出去
//        UserVO userVO = BeanUtil.copyProperties(user, UserVO.class);
        UserVO userVo = userService.selectUserAndAddressById(id);
            return Result.success(userVo);
    }

    @ApiOperation("根据id批量查询用户信息和地址")
    @GetMapping("/selectByIds")
    public Result selectByIds(@ApiParam("用户id集合") @RequestParam("ids") List<Long> ids) {
//        List<User> users = userService.listByIds(ids);
//        List<UserVO> userVos = BeanUtil.copyToList(users, UserVO.class);
        List<UserVO> userVos = userService.selectUserAndAddressByIds(ids);
        return Result.success(userVos);
    }

    @ApiOperation("根据id扣减余额")
    @PutMapping("/{id}/deduction/{money}")
    public Result deductionBalance(
            @ApiParam("用户id") @PathVariable("id") Long id,
            @ApiParam("扣减的金额") @PathVariable("money") Integer money) {
        String msg = userService.deductionBalance(id, money);
        if (msg == null) {
            return Result.success();
        }
        return Result.error(msg);
    }

    @ApiOperation("根据复杂条件查询")
    @GetMapping("/select/Conditions")
    public Result selectConditions(UserQuery userQuery) {
        List<UserVO> users = userService.selectConditions(userQuery.getName(), userQuery.getStatus(), userQuery.getMaxBalance(), userQuery.getMinBalance());
        return Result.success(users);
    }

    @ApiOperation("用户分页查询")
    @GetMapping("/page")
    public Result selectPage(UserQuery userQuery){
       PageDTO<UserVO> users = userService.selectPage(userQuery);
        return Result.success(users);
    }
}

